//////////////////////// Page Initializing ///////////////////
$(document).ready(function(){
	$("div#links a").click(function(){
		if($(this).hasClass('active')){
			return false;
		}
		$("div#links a.active").removeClass('active');
		$(this).addClass('active');
	});	
});

//////////////////////// LOADING A PAGE //////////////

function loadPage(sourceDiv, actionURL, serviceId) {	
		
	$.ajax ({		
		url:	  	actionURL + "?set_ajax_view",
		data:		"service_id=" + serviceId,		
		dataType: 	"html",
		beforeSend: function() {	
		},
		complete: 	function() {
			$('div#' + sourceDiv).show("fast");			
		},
		success:  function(data) {
			$("#" + sourceDiv).html(data);			
		}
	});		
}

/////////////// ENABLE CHECKBOX //////////////////////
function enableTypeSelect(id, checked){
	var selectTypeId	=	id + '_surgeryType';
	var typeSelect	=	$('#' + selectTypeId);
	var ref	=	"javascript: popUpAddService(" + id + ",'" + $('#' + id + ' :nth-child(2)').text() + "')";
	if(checked){
		typeSelect.removeAttr('disabled');
		$('#' + id + '_detail').attr('href', ref);
	}
	else{
		typeSelect.attr('disabled','disabled');
		$('#' + id + '_detail').attr('href', '#');
	}
}


///////////////// LIST SERVICE by DEPARTMENT ///////////////////

function listService() {
	$('#current_dept').text($('#departmentList :selected').attr('label'));	
	$.ajax ({		
		url		:	"listServiceByDepartmentAjax?set_ajax_view",
		data	:	{department_id : $('#departmentList').val()},
		dataType:	'json',
		beforeSend:	function() {	
			$('div#search_button').hide();
			$('div#search_button').next().show();												
		},
		complete: 	function() {						
			$('div#search_button').next().hide();
			$('div#search_button').show();
		},
		success	:  function(data) {
			serviceTable = $('#service_list');
			fillTableData(serviceTable, data.body);						
			
			$("span#total").text(data.total);				
			$('#service_container').show();		
		}
	});	
}

////////////////LIST SERVICE by SURGERY TYPE or SURGERY GROUP///////////////////
function listSurgeryServiceInType() {
	$('#current_dept').text(_('Available surgery services'));	
	$.ajax ({		
		url		:	"ListSurgeryServiceInTypeAjax?set_ajax_view",
		data	:	{type : $('#type').val()},
		dataType: 	"json",
		beforeSend: function() {	
			$('div#search_button').hide();
			$('div#search_button').next().show();												
		},
		complete: 	function() {						
			$('div#search_button').next().hide();
			$('div#search_button').show();
		},
		success	:  function(data) {
			serviceTable = $('#service_list');
			fillTableData(serviceTable, data.body);
									
			$("span#totalSurService").text(data.total);				
			$('#service_container').show();		
		}
	});	
}

function listSurgeryServiceInGroup(){
	if($('#selectedCatergoryId').val() == ''){
		return;
	}
	var selectedId	=	$('#selectedCatergoryId').val() + '_select';
	$.ajax({
		url		:	"ListSurgeryServiceInGroupAjax?set_ajax_view",
		data	:	{group_id : $('#' + selectedId).val()},
		dataType:	'json',
		beforeSend: function() {	
			$('div#search_button').hide();
			$('div#search_button').next().show();												
		},
		complete: 	function() {						
			$('div#search_button').next().hide();
			$('div#search_button').show();
		},
		success	:	function(data){
			if(data == 'eror'){
				return;
			}
			serviceTable = $('#service_list');
			fillTableData(serviceTable, data.body);
								
			$("span#totalSurService").text(data.total);				
			$('#service_container').show();
		}
	});
}

//////////////// EDIT, SAVE and CANCEL SURGERY SERVICE TYPE //////////////
function editSurgeryServiceType(servId){
	$('#' + servId + '_type').toggle();
	$('#' + servId + '_type_select').toggle();
	
	var htmlData ="<a class='btn' href='javascript:saveEditSurgeryServiceType(" + servId + ")' title='" + _('Save') + "'>"
		+			"<img src='../images/yes.png' style='width:24px;height:24px' alt='" + _('Save') + "'></img>"
		+		"</a>"
		+		"<a class='btn' href='javascript:cacelEditSurgeryServiceType(" + servId + ")' title='" + _('Cancel') + "'>"
		+			"<img src='../images/no.png' style='width:24px;height:24px' alt='" + _('Cancel') + "'></img>"
		+		"</a>";
	$('#service_list tr#' + servId + ' td:nth-child(4)').html(htmlData);
}

function saveEditSurgeryServiceType(servId){
	$.ajax({
		type	:	'POST',	
		url		:	'SaveSurgeryServiceTypeAjax?set_ajax_view',
		data	:	{service_id : servId, type : $('#' + servId + '_type_select').val()},
		complete:	function(){
			$('#' + servId + '_type').toggle();
			$('#' + servId + '_type_select').toggle();
			var htmlData = "<a class='btn' href='javascript:editSurgeryServiceType(" + servId + ")' title='" + _('Edit') + "'>"
				+			"<img src='../images/msg_edit.png' style='width:24px;height:24px' alt='" + _('Edit') + "'></img>"
				+		"</a>";
			$('#service_list tr#' + servId + ' td:nth-child(4)').html(htmlData);
		},
		success	:	function(surgeryType){
			$('#' + servId + '_type').html(surgeryType);
		},
		dataType:	'json'
	});
}

function cacelEditSurgeryServiceType(servId){
	$('#' + servId + '_type').toggle();
	$('#' + servId + '_type_select').toggle();
	
	var htmlData = "<a class='btn' href='javascript:editSurgeryServiceType(" + servId + ")' title='" + _('Edit') + "'>"
		+			"<img src='../images/msg_edit.png' style='width:24px;height:24px' alt='" + _('Edit') + "'></img>"
		+		"</a>";
	$('#service_list tr#' + servId + ' td:nth-child(4)').html(htmlData);
}

////////////////// CHANGE SURGERY GROUP AND SUB-GROUP ////////////////////
function changeSurgeryGroup(catergoryId, surfix, filter, hiddenField){
	var selectedId = catergoryId + surfix; 
	var groupFilter = $('#' + filter + '> *');
	for(i = 0; i < groupFilter.length; i++){
		if(groupFilter[i].id == selectedId){
			groupFilter[i].style.display = 'block';
		}
		else{
			groupFilter[i].style.display = 'none';
		}
	}
	$('#' + hiddenField).val(catergoryId);
}

////////////////// ADD NEW SURGERY SERVICE ////////////////////
function popUpAddService(servId, servName){
	// pop up dialog
	$('#service_id').val(servId);
	$('#service_name_span').html(servName);
	var buttons = {};
	buttons[_('Cancel')] = function() {	
		resetDialog();
		$(this).dialog('close');
	};
	buttons[_('Submit')] = function() {	
		submitAddNewSurgeryService();
		$(this).dialog('close');
	};
	var title = _('ADD SURGERY SERVICE');
	generalDialog('add_dialog',title,null,null,{
		autoOpen: true,			
		bgiframe: true,
		resizable: true,		
		width: 600,
		height: 450,
		modal: true,
		buttons:buttons
	});	
}

function submitAddNewSurgeryService(){
	var selectedId=$('#group_id').val() + '_elem_select';
	$.ajax({
		type	:	'POST',
		url		:	'SaveNewSurgeryServiceAjax?set_ajax_view',
		data	:	{
					service_id	:	$('#service_id').val(),
					group_id	:	$('#' + selectedId).val(),
					type_key	:	$('#type').val(),
					allowance_class : $('#allowance_class').val(),
					allowance	:	$('#allowance').val()
					},
		success:	function(data){
						listService();
						resetDialog();
					}
	});
}

//////////////// EDIT SURGERY SERVICE GROUP//////////////////
function popUpEditGroup(servId, servName, catergoryId, groupId, allowanceClass, allowance){
	//1. edit information according to selected service
	$('#' + catergoryId + '_radio').attr('checked', 'checked');
	$('#' + catergoryId + '_elem_div').show();
	$('#' + catergoryId + '_elem_select').val(groupId);
	$('#group_id').val(catergoryId);
	$('#allowance_class').val(allowanceClass);
	$('#allowance').val(allowance);
	
	//2. pop up edit dialog
	$('#service_id').val(servId);
	$('#service_name_span').html(servName);
	var buttons = {};
	buttons[_('Cancel')] = function() {	
		resetDialog();
		$(this).dialog('close');
	};
	buttons[_('OK')] = function() {	
		submitChangeSurgeryServiceGroup();
		$(this).dialog('close');
	};
	var title = _('EDIT SURGERY SERVICE GROUP');
	generalDialog('edit_group_dialog',title,null,null,{
		autoOpen: true,			
		bgiframe: true,
		resizable: true,		
		width: 600,
		height: 450,
		modal: true,
		buttons:buttons
	});	
}

function submitChangeSurgeryServiceGroup(){
	var selectedId=$('#group_id').val() + '_elem_select';
	$.ajax({
		type	:	'POST',
		url		:	'SaveChangeSurgeryServiceAjax?set_ajax_view',
		data	:	{
					service_id	:	$('#service_id').val(),
					group_id	:	$('#' + selectedId).val(),
					allowance_class : $('#allowance_class').val(),
					allowance	:	$('#allowance').val()
					},
		success:	function(data){
						listSurgeryServiceInGroup();
						resetDialog();
					}
	});
}

function resetDialog(){
	// Reset dialog
	$('#' + $('#group_id').val() + '_radio').removeAttr('checked');
	$('#' + $('#group_id').val() + '_elem_div').hide();
	$('#' + $('#group_id').val() + '_elem_select').val('');
	$('#allowance_class').val('');
	$('#allowance').val('');
	if($('#type') != null){
		$('#type').val('');
	}
}

/////////////// FILL DATA and STYLE SERVICE TABLE ///////////////////

function fillTotalTableData(parentDiv, data) {
	$('table.total_table', parentDiv).remove();
	parentDiv.append(data);
	parentDiv.children('table:last-child').addClass('total_table');
}

function fillTableData(table, data) {
	$('tbody', table).children().remove();	
	$('tbody', table).append(data);		
}
